#import "BlePlugin.h"
#import <BLE_Configuration_SDK/BLEConfigurationManager.h>
#import <BLE_Configuration_SDK/LogManager.h>
#import "ble_plugin.h"




@implementation BlePlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
  FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"ble_plugin"
            binaryMessenger:[registrar messenger]];
  BlePlugin* instance = [[BlePlugin alloc] init];
    SetUpEmindHomeBlePlugin(registrar.messenger, instance);
    
}

///初始化, 开始设置 Appkey
- (void)initAndSetAppKeyAppKey:(NSString *)appKey completion:(void (^)(NSNumber *_Nullable, FlutterError *_Nullable))completion{
    [[BLEConfigurationManager shared] setupWithAppKey:appKey result:^(BOOL isSuccessful) {
                completion(@(isSuccessful),nil);
            }];
    DLog(@"initAndSetAppKeyAppKey==>%@",appKey);
    
}


///开始配网
- (void)startConfigurationRequest:(nonnull StartConfigurationRequest *)request completion:(nonnull void (^)(NSNumber * _Nullable, FlutterError * _Nullable))completion {
    DLog(@"startConfigurationRequest");
    
    [[BLEConfigurationManager shared] startConfigurationManagerWithSn:request.sn ssid:request.ssid password:request.password log:^(Log *log) {
            // 处理日志
        } result:^(BluetoothCodeType code) {
            completion(@(code),nil);
        }];
}
/// 是否初始化成功
///
/// @return `nil` only when `error != nil`.
- (nullable NSNumber *)getBLESetupFinishedWithError:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
    DLog(@"getBLESetupFinishedWithError");
    return  @([BLEConfigurationManager shared].isSetupFinished);
}
/// 蓝牙状态改变
///
/// @return `nil` only when `error != nil`.
- (nullable NSNumber *)getBluetoothStateWithError:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
    DLog(@"getBluetoothStateWithError");
    return  @([BLEConfigurationManager shared].bluetoothState);
}

/// 获取最后一次配网日志
///
/// @return `nil` only when `error != nil`.
- (nullable NSArray<LogMessage *> *)getLogsOfLastConfigurationWithError:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
    DLog(@"getLogsOfLastConfigurationWithError");
    NSArray *logs = [[BLEConfigurationManager shared] getLogsOfLastConfiguration];
        NSMutableArray<LogMessage *> *logList = [NSMutableArray array];
        for (Log *log in logs) {
            LogMessage *message = [[LogMessage alloc] init];
            message.message = log.message;
            message.dateTime =[BlePlugin dateToNSString:log.date];
            [logList addObject:message];
        }
    return  logList;
    
}

/// 蓝牙状态改变
///
/// @return `nil` only when `error != nil`
- (nullable NSString *)getBLEBluetoothStateDidChangeWithError:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
    DLog(@"getBLEBluetoothStateDidChangeWithError");
    return  BLEBluetoothStateDidChange;
}

///退出
- (void)exitConfigurationWithError:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
    DLog(@"exitConfigurationWithError");
    [[BLEConfigurationManager shared] exitConfigurationManager];
}
/// 添加日志
/// message  日志信息
///
/// @return `nil` only when `error != nil`.
- (nullable LogMessage *)addLogWithMessageMessage:(nonnull NSString *)message error:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
    DLog(@"addLogWithMessageMessage==>%@",message);
    
   Log *logMsg = [[LogManager shared]addLogWithMessage:message];
    NSString *dateStr = [NSString stringWithFormat:@"%@",logMsg.date];
    @try {
        dateStr =[BlePlugin dateToNSString:logMsg.date];
    } @catch (NSException *exception) {
        
    } @finally {
        
    }
    return  [LogMessage makeWithMessage:logMsg.message dateTime:dateStr];
}

/// 获取所有的日志
///
/// @return `nil` only when `error != nil`.
- (nullable NSArray *)getAllLogWithError:(FlutterError *_Nullable *_Nonnull)error{
    return  [[LogManager shared] getAllLogMessages];
}

/// 获取所有的日志内容
///
/// @return `nil` only when `error != nil`
- (nullable NSArray *)getAllLogMessagesWithError:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
    DLog(@"getAllLogMessages");
//    NSArray *logs = [[LogManager shared] getAllLogs];
//        NSMutableArray<LogMessage *> *logList = [NSMutableArray array];
//        for (Log *log in logs) {
//            LogMessage *message = [[LogMessage alloc] init];
//            message.message = log.message;
//            [logList addObject:message];
//        }
    return  [[LogManager shared] getAllLogs];
}



/// 获取BLE版本
///
/// @return `nil` only when `error != nil`.
- (nullable NSString *)getBLEVersionWithError:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
    DLog(@"getBLEVersion");
    return  [[BLEConfigurationManager shared]version];
}



///删除所有的日志
- (void)removeAllLogsWithError:(FlutterError * _Nullable __autoreleasing * _Nonnull)error {
    [[LogManager shared]removeAllLogs];
    DLog(@"removeAllLogs");
}




+(NSString *)dateToNSString:(NSDate *)date{
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd-HH-mm-ss"];
    NSString * strDate = [formatter stringFromDate:date];
    return strDate;
}




//- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
//  if ([@"getPlatformVersion" isEqualToString:call.method]) {
//    result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
//  } else {
//    result(FlutterMethodNotImplemented);
//  }
//}

@end
